//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using OpenLS.Drawing;
using OpenLS.Drawing.UI;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class TableBordersControl: TableControl
    {
        protected override void OnRender(DrawingContext drawingContext)
        {
            Table table = Table;
            if (table == null)
                return;
            if (table.Style == null)
                return;
            Worksheet worksheet = SpreadsheetElement.GetWorksheet(this);
            ColorScheme colorScheme = DrawingElement.GetColorScheme(this);
            if (previousScheme != colorScheme)
            {
                previousScheme = colorScheme;
                pens = new Dictionary<Pair<SpreadsheetColor, BorderStyle>, Pen>();
            }


            if (table.Style.WholeTable != null && table.Style.WholeTable.StyleRecord.Borders != null)
            {
                Rect rect = GetRect(table.Range.TopLeftCell.Row.Index, table.Range.TopLeftCell.Column.Index,
                                    table.Range.TopLeftCell.Row.Index + table.Range.RowSpan,
                                    table.Range.TopLeftCell.Column.Index + table.Range.ColumnSpan, worksheet);
                BorderDrawer.DrawBorders(drawingContext, table.Style.WholeTable.StyleRecord.Borders,
                                         rect,
                                         colorScheme, GetPen);
                Border h = table.Style.WholeTable.StyleRecord.Borders.Horizontal;
                if (BorderDrawer.HasBorder(h))
                {
                    for (int row = table.Range.TopLeftCell.Row.Index + 1; row < table.Range.TopLeftCell.Row.Index + table.Range.RowSpan; row++)
                    {
                        double y = GetY(worksheet, row);
                        BorderDrawer.DrawLine(drawingContext, GetPen(h, colorScheme), new Point(rect.Left, y), new Point(rect.Right, y));
                    }
                }
                Border v = table.Style.WholeTable.StyleRecord.Borders.Vertical;
                if (BorderDrawer.HasBorder(v))
                {
                    for (int column = table.Range.TopLeftCell.Column.Index + 1; column < table.Range.TopLeftCell.Column.Index + table.Range.ColumnSpan; column++)
                    {
                        double x = GetX(worksheet, column);
                        BorderDrawer.DrawLine(drawingContext, GetPen(h, colorScheme), new Point(x, rect.Top), new Point(x, rect.Bottom));
                    }
                }

            }
        }
        internal Pen GetPen(Border border, ColorScheme colorScheme)
        {
            Pen result;
            if (pens.TryGetValue(new Pair<SpreadsheetColor, BorderStyle>(border.Color, border.Style), out result))
                return result;
            result = new Pen(new SolidColorBrush(border.GetWPFColor(colorScheme)),
                             BorderStyleToThicknessConverter.StyleToThickness(border.Style))
                         {
                             DashStyle =
                                 BorderStyleToDashConverter.StyleToDash(border.Style),
                             EndLineCap = PenLineCap.Square,
                             StartLineCap = PenLineCap.Square,
                             DashCap = PenLineCap.Flat
                         }; //\\ missing dash;
            if (result.CanFreeze)
                result.Freeze();
            pens.Add(new Pair<SpreadsheetColor, BorderStyle>(border.Color, border.Style), result);
            return result;
        }
        private Dictionary<Pair<SpreadsheetColor, BorderStyle>, Pen> pens =
            new Dictionary<Pair<SpreadsheetColor, BorderStyle>, Pen>();

        private ColorScheme previousScheme;

    }
}